#ifndef __RS485_protocol__H
#define __RS485_protocol__H

#ifdef __cplusplus
extern "C"
{
#endif

#include "stdbool.h"

#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"

#include "A_device.h"

#include "time.h"

#include "common.h"

#define placeholder 0x00
#define defaultValue 0x00

#define CMD_sys_info1 0x00
#define CMD_sys_info2 0xF1
#define CMD_key_board_info 0xB0
    // #define CMD_key_board_info 0xB1

    typedef struct
    {
        /* data */
        unsigned char floor1 : 1;
        unsigned char floor2 : 1;
        unsigned char floor3 : 1;
        unsigned char floor4 : 1;
        unsigned char floor5 : 1;
        unsigned char floor6 : 1;
        unsigned char floor7 : 1;
        unsigned char floor8 : 1;
        unsigned char floor9 : 1;
        unsigned char floor10 : 1;
        unsigned char floor11 : 1;
        unsigned char floor12 : 1;
        unsigned char floor13 : 1;
        unsigned char floor14 : 1;
        unsigned char floor15 : 1;
        unsigned char floor16 : 1;
    } floor_status_bit;

    typedef union
    {
        floor_status_bit value_bit;
        unsigned short value_short;
    } floor_status_u;

#define get_485Device get485_device // 485_1

    typedef enum
    {
        PRO_OK = 0,
        PRO_ERR,
        PRO_Lattice_Disconnect,
        PRO_NotInRange,
        PRO_NOT_SUPPORT,
        PRO_NOT_READY,
        PRO_KeyFormatErr,
        PRO_Ctrl_Data_Err,
        PRO_SPI_Flash_useless,
        PRO_SUCESS = 0xA1,
    } pro_ret;

#define HANDLER(name) static pro_ret protocol_handler_##name(const unsigned char *const data)
#define HANDLER_POINT(name) protocol_handler_##name

#pragma pack(1)

    typedef struct
    {
        /* data */
        unsigned char direction : 1;
        unsigned char direction_ctrl : 2;
        unsigned char overLoad : 1;
        unsigned char full : 1;
        unsigned char err : 1;
        unsigned char jx : 1;
        unsigned char fire : 1;
    } elevator_status;

    typedef union 
    {
        /* data */
        elevator_status info;
        unsigned char data;
    }sys_info_u;
    


    typedef struct
    {
        sys_info_u info;
        unsigned char reserve1;
        unsigned char dis_info[3];
        unsigned char reserve3;

    } sys_info_data_s;
#pragma pack()

    typedef struct
    {
        /* data */
        unsigned char cmd;
        unsigned char len;
    } msg_content_len_s;
    ;

    typedef pro_ret (*protocol_handler)(const unsigned char *const data);

    /*
     * 协议命令字对应handler函数的结构体
     * cmd：协议命令字
     * handler：处理函数
     */
    typedef struct
    {
        unsigned char cmd;
        protocol_handler handler;
    } _protocol_struct;

    void frame_parse(void);

#ifdef __cplusplus
}
#endif

#endif
